<?php
namespace app\admin\controller;
use catetree\Catetree;
class AuthRule extends Common
{
    public function lst()
    {
    	$_ruleRes=db('auth_rule')->where('status','>','-1')->order('sort')->select();
        $CateTree=new Catetree();
    	$ruleRes=$CateTree->catetree($_ruleRes);

        $count=db('auth_rule')->count();
        $this->assign([
            'ruleRes'=>$ruleRes,
            'count'=>$count,
        ]);
        return view();
    }

    public function add()
    {
    	if(request()->isAjax()){
    		$data=input('post.')['data'];
            $name=db('auth_rule')->where('name',$data['name'])->select();
            if($name){
                return ['msg'=>'规则不能重复','icon'=>2];
            }
    		$rule_id=db('auth_rule')->insertGetId($data);
            // 给超级管理员用户组加当前规则权限
            $group_id=db('auth_group_access')->field('group_id')->where('uid',session('uid','','admin'))->find();
            $groupRule=db('auth_group')->field('rules')->find($group_id['group_id']);
            $groupRules=$groupRule['rules'].','.$rule_id;
            db('auth_group')->where('id',$group_id['group_id'])->update(['rules'=>$groupRules]);
    		if($rule_id){
                return ['msg'=>'新增规则成功','icon'=>1];
            }else{
                return ['msg'=>'新增规则失败','icon'=>2];
            }
    		return;
    	}
        //列表页选择新增子规则的时候

        //用于上级规则的显示
    	$_ruleRes=db('auth_rule')->order('sort')->select();
        $CateTree=new Catetree();
        $ruleRes=$CateTree->catetree($_ruleRes);
        //lst页面点击添加子规则的情况
        $ruleid=input('rule_id');
        $this->assign([
            'ruleRes'=>$ruleRes,
            'ruleid'=>$ruleid,
        ]);
        return view();
    }

    public function edit()
    {
    	if(request()->isAjax()){
    		$data=input('post.')['data'];
            $save=db('authRule')->update($data);
            if($save !== false){
                return ['msg'=>'编辑规则成功','icon'=>1];
            }else{
                return ['msg'=>'编辑规则失败','icon'=>2];
            }
    		return;
    	}
        //用于上级规则的显示
        $_ruleRes=db('auth_rule')->order('sort')->select();
        $CateTree=new Catetree();
        $ruleRes=$CateTree->catetree($_ruleRes);
        //当前这条规则的信息
        $rules=db('authRule')->find(input('id'));
        $this->assign([
            'ruleRes'=>$ruleRes,
            'rules'=>$rules,
        ]);
    	return view();
    }

    public function del()
    {
        if(request()->isAjax()){
            $id=input('id');
            $CateTree=new Catetree();
            $allId=$CateTree->childrenids('auth_rule',$id);
            $allId[]=$id;
            $del=db('auth_rule')->delete($allId);
            if($del){
                return ['msg'=>'删除规则成功','icon'=>1];
            }else{
                return ['msg'=>'删除规则失败','icon'=>2];
            }
        } 
    }

    public function delAll()
    {
        if(request()->isAjax()){
            $data=input('post.')['data'];
            foreach ($data as $k => $v){
                $CateTree=new Catetree();
                $allId=$CateTree->childrenids('auth_rule',$v,true);
                $allId[]=$v;
                db('auth_rule')->delete($allId);
            }
            return ['msg'=>'批量删除规则成功','icon'=>1];
        }
        
    }

    //ajax更新开启状态
    public function changeStatus()
    {
        if(request()->isAjax()){
            $id=input('id');
            $menu=input('menu');
            if($menu == 1){
                db('auth_rule')->where(['id'=>$id])->update(['menu'=>0]);
                return ['msg'=>'已隐藏','icon'=>2];
            }else{
                db('auth_rule')->where(['id'=>$id])->update(['menu'=>1]);
                return ['msg'=>'已显示','icon'=>1];
            }
        }else{
            $this->error("非法操作！");
        }
    }
  	//栏目排序
    public function sort()
    {
        if(request()->isAjax()){
            $id=input('id');
            $sort=input('sort');
            $save=db('auth_rule')->where(['id'=>$id])->update(['sort'=>$sort]);
            if($save !== false){
                return ['msg'=>'已排序','icon'=>1];
            }
        }

    }




}
